// createElement 函数
export function createElement(type, attributes, ...children) {
  // 创建节点
  let element

  if(typeof type === 'string') element =new ElementWrapper(type)
  else element = new type
  // 设置属性
  for (const name in attributes) {
    element.setAttribute(name, attributes[name])
  }
  // 把子元素加上
  for (const child of children) {
    // 如果是文本字符串，转换成文本节点
    if(typeof child === 'string') child = new TextWrapper(child)
    element.appendChild(child)
  }
  // 返回 dom 
  return element;
}

export class Component{
  constructor (type) {
  }

  setAttribute(name, value) {
    this.root.setAttribute(name,value)
  }
  appendChild(child) {
    child.mountTo(this.root)
  }
  mountTo(parent) {
    parent.appendChild(this.root)
  }
}

class ElementWrapper extends Component {
  constructor(type) {
    this.root = document.createElement(type)
  }
  
}

class TextWrapper extends Component{
  constructor(content) {
    this.root = document.createTextNode(content)
  }
  
}


